<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Cache;
use think\Db;
use think\Exception;
use think\exception\ValidateException;
use think\Log;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Village extends Backend
{

    /**
     * Village模型对象
     * @var \app\admin\model\Village
     */
    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Village;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //分页
            $limit = $this->request->request("limit");
            $offset = $this->request->request("offset");
            //下拉分页
            $pageNumber = $this->request->request("pageNumber");
            $pageSize = $this->request->request("pageSize");
            //编辑访问
            $searchKey = $this->request->request("searchKey");
            $searchValue = $this->request->request("searchValue");
            //搜索访问
            $filter = $this->request->request('filter');

            $this->model = model('\app\admin\model\Village');

            if ($searchKey != null && $searchValue != null) {//编辑访问
                $row = $this->model
                    ->where([$searchKey => $searchValue])
                    ->find();
                return json(["rows" => $row]);
            } elseif ($filter != '{}' && $filter) {  //搜索访问

                $tmpWhereList = json_decode($filter, true);
                $whereList = [];
                foreach ($tmpWhereList as $key => $item) {
                    $whereList['fa_village.' . $key] = $item;
                }
                foreach ($whereList as $key => $item) {
                    $whereList[$key] = ['like', '%' . $item . '%'];
                }

                $list = $this->model
                    ->with(["worker_group" => function ($query) {
                        $query->withField('name');
                    }])
                    ->where('deletetime', null)
                    ->where($whereList)
                    ->page($pageNumber, $pageSize)
                    ->select();
                $total = $this->model
                    ->with(["worker_group" => function ($query) {
                        $query->withField('name');
                    }])
                    ->where('deletetime', null)
                    ->where($whereList)
                    ->page($pageNumber, $pageSize)
                    ->select();
                $result = array("total" => $total, "rows" => $list);
                return json($result);
            } else {
                if ($pageNumber != null && $pageSize != null) {//下拉分页
                    $list = $this->model
                        ->with(["worker_group" => function ($query) {
                            $query->withField('name');
                        }])
                        ->where('deletetime', null)
                        ->page($pageNumber, $pageSize)
                        ->select();


                } else {//查看分页
                    $list = $this->model
                        ->with(["worker_group" => function ($query) {
                            $query->withField('name');
                        }])
                        ->where('deletetime', null)
                        ->page((int)$offset / $limit + 1, $limit)
                        ->select();
                }
            }

            $total = $this->model
                ->with(["worker_group" => function ($query) {
                    $query->withField('name');
                }])
                ->where('deletetime', null)
                ->count();


            $result = array("total" => $total, "rows" => $list);

            return json($result);

        }
        return $this->view->fetch();
    }

}
